/*
 * $HeadURL: $
 * $Id: $
 * Copyright (c) 2013 by Wodensoft System, all rights reserved.
 */
package com.wodensoft.core.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态PO代理.
 * 
 * @author Woden
 * 
 */
public class DataPO {

    private static final String ENTRY_NAME_KEY = "$type$";

    private Map<String, Object> po;

    public String getEntryName() {
        return getString(ENTRY_NAME_KEY);
    }

    public DataPO(String entryName) {
        this(entryName, new HashMap<String, Object>());
    }

    public DataPO(String entryName, Map<String, Object> po) {
        this.po = po;
        this.po.put(ENTRY_NAME_KEY, entryName);
    }

    public void set(String name, Object value) {
        po.put(name, value);
    }

    /**
     * 一对多关系仲的<code>Map</code>结构.
     * 
     * @param entryName
     * @param name
     * @param value
     */
    public void addSubEntry(String entryName, String index, Map<String, Object> value) {
        if (!po.containsKey(entryName)) {
            po.put(entryName, new HashMap<String, Map<String, Map<String, Object>>>());
        }
        Map<String, Map<String, Object>> subs = (Map<String, Map<String, Object>>) getSubEntrys(entryName);
        subs.put((String) value.get(index), value);
    }

    /**
     * 一对多关系<code>List</code>结构.
     * 
     * @param entryName
     * @param value
     */
    public void addSubItem(String entryName, Map<String, Object> value) {
        if (!po.containsKey(entryName)) {
            po.put(entryName, new ArrayList<Map<String, Object>>());
        }
        List<Map<String, Object>> subs = (List<Map<String, Object>>) getSubList(entryName);
        subs.add(value);
    }

    public Object get(String name) {
        return po.get(name);
    }

    public String getString(String name) {
        return (String) get(name);
    }

    public Long getLong(String name) {
        return (Long) get(name);
    }

    public Integer getIntger(String name) {
        return (Integer) get(name);
    }

    public Date getDate(String name) {
        return (Date) get(name);
    }

    public BigDecimal getMoney(String name) {
        return (BigDecimal) get(name);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Map<String, Object>> getSubEntrys(String name) {
        return (Map<String, Map<String, Object>>) get(name);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getSubList(String name) {
        return (List<Map<String, Object>>) get(name);
    }

    /**
     * 将动态PO包装类转换成MAP(直接放到dao层处理).
     * 
     * @return 动态对象<code>Map</code>.
     */
    public final Map<String, Object> toEntry() {
        return po;
    }
}
